package d0427.wordgame.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.wb.swt.SWTResourceManager;

import d0427.wordgame.Game;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Color;

public class WordGameWin {

	protected Shell shell;
	// 实例变量(全局变量)
	Label label_1;
	Composite composite;
	Composite composite_1;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			WordGameWin window = new WordGameWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		label_1 = new Label(shell, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		label_1.setText("第几关");
		
		composite = new Composite(shell, SWT.BORDER);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new RowData(422, 69));
		
//		Label lblA = new Label(composite, SWT.NONE);
//		lblA.setFont(SWTResourceManager.getFont("微软雅黑", 14, SWT.NORMAL));
//		lblA.setText("A");
		
		composite_1 = new Composite(shell, SWT.BORDER);
		composite_1.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_1.setLayoutData(new RowData(421, 79));
		
		
		/**
		 * 我的代码
		 */
		initLevel();
		
		/**
		 * 监听窗口键盘按下事件
		 */
		shell.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				
				if ( game.input(e.character) == true ) {
					// 输出label 控件
					Label lblA = new Label(composite_1, SWT.NONE);
					lblA.setFont(SWTResourceManager.getFont("微软雅黑", 14, SWT.NORMAL));
					lblA.setText(e.character + "");  // 将 c 转成字符串, 输出到控件上
					// 刷新容器控件
					composite_1.layout();
					
					// 找到到当前字符的label
					Control[] controls = composite.getChildren(); // 获取所有子控件
					int index = game.getIndex();
					index --;
					Label label = (Label)controls[index];
					// 构建颜色对象
					Color color = SWTResourceManager.getColor(SWT.COLOR_GREEN);
					// 设置字体颜色
					label.setForeground(color);
					
					if(game.isOver()) {
						MessageBox mb = new MessageBox(shell);
						mb.setText("系统提示");
						mb.setMessage("You Win!");
						mb.open();
						// 开启下一关
						initLevel();
					}
				} 
			}
		});
		
	}
	
	/**
	 * 我的代码
	 */
	Game game = new Game();
	
	/**
	 * 初始化当前关
	 */
	public void initLevel() {
		label_1.setText("第"+game.getPlayer().getLevelNo()+"关");

		// 清空composite内部控件
		for(Control con : composite.getChildren()) {
			con.dispose();
		}
		// 清空composite_1内部控件
		for(Control con : composite_1.getChildren()) {
			con.dispose();
		}

		// 获取当前关字符串
		String words = game.getWords();
		// 转字符数组
		char[] chars = words.toCharArray();// 将字符串转成字符数组
		for(char c : chars) {
			Label lblA = new Label(composite, SWT.NONE);
			lblA.setFont(SWTResourceManager.getFont("微软雅黑", 14, SWT.NORMAL));
			lblA.setText(c + "");  // 将 c 转成字符串, 输出到控件上
		}
		
		// 使用一布局方法重新绘制容器控件
		composite.layout();
		composite_1.layout();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
